classdef Underlying < handle
    properties (SetAccess = private)
        fileNameConst = 'underlying';
        fileName = '';
        
        primeTraderTicker = '';
    end
    
    methods 
        %%
        function obj = Underlying( ticker )
            obj.primeTraderTicker = ticker;
            obj.fileName = strcat( obj.fileNameConst, datestr(today), '.csv' );
            writeChangesToFile( obj.fileName, {[datestr(now) ,' ', obj.primeTraderTicker]}, 'w' );

            obj.sampleUnderlying();
        end
        
        
        %%
        function sampleUnderlying( obj )
            %Read data (localhost is set up separately by PrimeTrader software)
            try
                urlString = urlread( ['http://localhost:8678/quotes/', obj.primeTraderTicker] );
            catch
                error( 'Error reading data. Make sure url string is correctly specified and that PrimeTrader is running')
            end

            lineBreakIndices =  strfind( urlString, '</tr>');
            line = urlString( lineBreakIndices(1):end );    

            td_start = strfind( line, '<td>' );
            td_end =  strfind( line, '</td>' );
        
            %Open, high, low, last
            tempStr = [ line( td_start(14)+4:td_end(14)-1 ), ',',  line( td_start(15)+4:td_end(15)-1 ), ',' ...
                ,line( td_start(16)+4:td_end(16)-1 ), ',', line( td_start(17)+4:td_end(17)-1 )];
            
            writeChangesToFile( obj.fileName, {tempStr}, 'a' );
        end
        

    end
end
    